//获取数据并渲染
function getAtricleCat() {
    $.ajax({
        url: "/my/article/cates",
        success: function(res) {
            var str = template("article-content", res)
            $("tbody").html(str)
        }
    })
}
getAtricleCat();
//添加类别
var index;
$(".addCat").on("click", function() {
    index = layer.open({
        title: "添加分类",
        btn: null,
        content: template("article-cat", {}),
        area: ["500px", "250px"]
    })
})
$("body").on("submit", ".add-form", function(e) {
    e.preventDefault();
    $.ajax({
        url: "/my/article/addcates",
        method: "POST",
        data: $(this).serialize(),
        success: function(res) {
            layer.msg(res.message);
            layer.close(index);
            getAtricleCat();

        }
    })
})

//编辑分类
$("tbody").on("click", ".edit", function() {
        var $this = $(this);
        index = layer.open({
            title: "编辑分类",
            content: template("edit-cate", {}),
            btn: null,
            area: ["500px", "250px"],
            success: function(res) {
                $.ajax({
                    url: "/my/article/cates/" + $this.attr("data-id"),
                    success: function(res) {
                        layui.form.val("edit", res.data)
                    }
                })
            }
        })
    })
    //提交功能
$("body").on("submit", ".edit-form", function(e) {
        e.preventDefault();
        $.ajax({
            url: "/my/article/updatecate",
            method: "POST",
            data: $(this).serialize(),
            success: function(res) {
                layer.msg(res.message);
                layer.close(index);
                getAtricleCat();

            }
        })
    })
    //删除
$("tbody").on("click", ".delete", function() {
    var id = $(this).attr("data-id");
    layer.confirm("要删除么", {
        icon: 3,
        title: "提示"
    }, function(index) {
        $.ajax({
            url: "/my/article/deletecate/" + id,
            success: function(res) {
                layer.msg(res.message)
                getAtricleCat();
            }
        })
        layer.close(index)
    })
})